/** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
**/

var MiniView = function () {
	this.container = get("screen_mv_item");
	this.logo = get("mv_offline_logo");
	this.pos = -1;
	this.currentItem = null;
	this.item_title = $("div.mv_title");
	this.item_subtitle = $("div.mv_subtitle");
	this.item_img = get("mv_image");
	this.item_img.error( function () { $(this).hide(); } );
	this.timeout = null;
}

MiniView.prototype.show = function () {
	if (this.timeout != null) {
		clearTimeout(this.timeout);
	}
	
	get("content_container").addClass("hidden");
	if (DEBUG) {
		get("debug_line").addClass("hidden");
		get("debug_view").addClass("hidden");		
	}
	$(document.body).addClass("miniview");
	this.container.hide();
	this.logo.show();
	this.update();
	var self = this;	
}

MiniView.prototype.update = function() {
	if (!isMiniView()) {
		return;
	}
	
	if (this.timeout != null) {
		clearTimeout(this.timeout);
	}
	
	var self = this;
	
	this.timeout = setTimeout( function() { self.update(); }, MINIVIEW_UPDATE_INTERVAL );
	
	// check if refresh is needed
	if( needRefresh() ) {
		this.refresh();
		return;
	}
	this.renderNextItem();
	
	clearTimeout(this.timeout);
	this.timeout = setTimeout( function() { self.update(); }, MINIVIEW_UPDATE_INTERVAL );
}

MiniView.prototype.hide = function () {
	if(this.timeout) {
		clearTimeout(this.timeout);
		this.timeout = null;
	}
	this.currentItem = null;
	
	get("content_container").removeClass("hidden");
	if (DEBUG) {
		get("debug_line").removeClass("hidden");
		get("debug_view").removeClass("hidden");		
	}	
	$(document.body).removeClass("miniview");
}

MiniView.prototype.refresh = function(){
	if (terms.isAccepted() && OnlineMonitor.isOnline()) {
		var self = this;
		downloadRss(RSS_URL, function(){
			self.pos = -1;
			self.currentItem = null;
			self.update();
		}, null, true);
	} else {
		this.render(null);
	}	
}

MiniView.prototype.renderNextItem = function () {
	this.pos++;
	if(this.pos >= miniViewItems.length)
		this.pos = 0;
	
	// do not re-render same object
	if( this.currentItem == miniViewItems[this.pos] ) {
		return;
	}
	
	this.currentItem = miniViewItems[this.pos];
	this.render( this.currentItem );
}

MiniView.prototype.render = function( item ){
	
	if (item != null) {
		this.item_title.html(item.title);
		this.item_subtitle.html(item.info ? item.info : "");
		
		//first hide the image
		this.item_img.hide();
		
		if (item.image && OnlineMonitor.isOnline()) {
			this.item_img.attr("src", item.image);
			this.item_img.load(function(){
				$(this).show();
			});
		}
		
		this.logo.hide();
		this.container.show();
	}
	else {
		this.container.hide();
		this.logo.show();
	}
}